﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace HotelManagement
{
    public partial class ServiceFRM : Form
    {
        SqlConnection Cnn = new SqlConnection(Globals.DBConnection());
        DataSet ds = new DataSet();
        SqlDataAdapter da;
        int s_id;
        public ServiceFRM()
        {
            InitializeComponent();
        }

        private void ServiceFRM_Load(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select s_id as[کد] ,s_name as [نام سرویس] ,s_price as[هزینه سرویس] from Service",Cnn);
            da.Fill(ds);
            DgViewService.DataSource = ds.Tables[0];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(button1.Text))
            {
                SqlCommand SaveINDB = new SqlCommand("INSERT Service (s_name,s_price) values (@name,@price)", Cnn);
                SaveINDB.Parameters.AddWithValue("@name", ServiceNameTxt.Text.Trim());
                SaveINDB.Parameters.AddWithValue("@price", ServicePriceTxt.Text.Trim());
               

                Cnn.Open();
                SaveINDB.ExecuteNonQuery();
                Cnn.Close();

                ds.Clear();
                da.Fill(ds);
                DgViewService.DataSource = ds.Tables[0];
            }
            else
            {
                MessageBox.Show("مقادیر خالی است");
            }
        }

        private void DeleteBtn_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("آیا برای حذف سرویس مطمئنید؟", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                SqlCommand DeleteCMD = new SqlCommand("DELETE FROM Service WHERE s_id=@s_id", Cnn);
                DeleteCMD.Parameters.AddWithValue("@s_id", s_id);
                Cnn.Open();

                try
                {
                    DeleteCMD.ExecuteNonQuery();
                }
                catch (SqlException)
                {
                    MessageBox.Show("این سرویس در حال استفاده است","خطا",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    Cnn.Close();
                    return;
                }
                finally
                {
                    Cnn.Close();

                }
                ds.Clear();
                da.Fill(ds);
                DgViewService.DataSource = ds.Tables[0];
                DeleteBtn.Enabled = false;
                MessageBox.Show("حذف سرویس با موفقیت انجام شد");

            }
        }

        private void DgViewService_CellMouseDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void DgViewService_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DeleteBtn.Enabled = true;
            s_id = (int)DgViewService.CurrentRow.Cells[0].Value;
        }


    }
}
